/**
 *
 */
package org.suren.littlebird.actions.logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Display;
import org.suren.littlebird.Activator;
import org.suren.littlebird.actions.Reload;
import org.suren.littlebird.po.SuRenLogger;
import org.suren.littlebird.preferences.PreferenceConstants;
import org.suren.littlebird.server.LoggerServer;
import org.suren.littlebird.server.LoggerServerFactory;

/**
 * @author suren
 * @since 2015-2-2
 */
public class SuRenLoggerReload implements Reload
{
	private TableViewer viewer;
	private IPreferenceStore store;
	private IStatusLineManager statusLineMgr;

	public SuRenLoggerReload(TableViewer viewer, IStatusLineManager iStatusLineManager)
	{
		this.store = Activator.getDefault().getPreferenceStore();
		this.viewer = viewer;
		this.statusLineMgr = iStatusLineManager;
	}

	@Override
	public void reload(Object source, Object data)
	{
		new Job("Logger List Reloading"){

			@Override
			protected IStatus run(IProgressMonitor arg0)
			{
				LoggerServer loggerServer = LoggerServerFactory.getInstance();
				boolean hidden = store.getBoolean(PreferenceConstants.P_LOGGER_NO_BRIDGE_HIDDEN);
				String filter = store.getString(PreferenceConstants.P_LOGGER_FILTER);
				List<List<Entry<String, String>>> loggers = null;

				if(filter == null || "".equals(filter))
				{
					loggers = loggerServer.getAllLoggers();
				}
				else
				{
					loggers = loggerServer.searchLoggersBy(filter);
				}
//				System.out.println(loggerServer.test() + "====");

				int len = loggers.size();
				final List<SuRenLogger> loggerList = new ArrayList<SuRenLogger>();
				for(int i = 0; i < len; ++i)
				{
					SuRenLogger surenLogger = new SuRenLogger();
					surenLogger.setIndex(i);

					List<Entry<String, String>> logger = loggers.get(i);
					for(Entry<String, String> entry : logger)
					{
						String bridgeCount = entry.getValue();

						if("name".equals(entry.getKey()))
						{
							surenLogger.setName(entry.getValue());
						}
						else if("level".equals(entry.getKey()))
						{
							surenLogger.setLevel(entry.getValue());
						}
						else if("bridges_count".equals(entry.getKey()))
						{
							surenLogger.setBridges(bridgeCount);
						}
					}

					try
					{
						int count = Integer.parseInt(surenLogger.getBridges());
						if(hidden && count <= 0)
						{
							continue;
						}
					}
					catch(NumberFormatException e){}

					loggerList.add(surenLogger);
				}

				Display.getDefault().syncExec(new Runnable()
				{

					@Override
					public void run()
					{
						viewer.setInput(loggerList);

						statusLineMgr.setMessage("Logger's Number is : " + loggerList.size());
					}
				});

				return Status.OK_STATUS;
			}
		}.schedule();
	}
}
